package com.vay.de.dang.online.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.FragmentTransaction;

import com.vay.de.dang.online.ui.bill.TabBillFragment;
import com.vay.de.dang.online.ui.home.TabHomeFragment;
import com.vay.de.dang.online.ui.sign.SignInActivity;
import com.vay.de.dang.online.util.LocationHelper;
import com.vay.de.dang.online.util.UpgradeHelper;
import com.vay.de.dang.online.util.VnContext;
import com.vay.de.dang.online.util.VnUIManager;
import com.vay.de.dang.online.view.DialogWarring;
import com.vay.de.dang.online.view.VnTabButton;
import com.vay.de.dang.online.ui.my.TabMyFragment;

import java.util.ArrayList;
import java.util.List;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.databinding.ActivityMainBinding;
import com.vay.de.dang.online.web.BaseApi;

public class HomeActivity extends BaseActivity {

    final static String[] TabKey = {"Home", "Bill", "Mine"};
    private static HomeActivity homeActivity;

    private ActivityMainBinding binding;
    private List<BaseFrag> frags = new ArrayList<>();
    private List<VnTabButton> vnTabButtons = new ArrayList<>();
    private int preIndex;
    private BaseFrag preFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        homeActivity = this;

        // 升级
        UpgradeHelper.init(this);

        frags.add(TabHomeFragment.newInstance());
        frags.add(TabBillFragment.newInstance());
        frags.add(TabMyFragment.newInstance());

        VnTabButton home = binding.tabHome.loadTab(R.mipmap.ic_tab_home_n, R.mipmap.ic_tab_home_p,
                getString(R.string.tab_home));
        home.setOnClickListener(v -> switchTab(0));
        home.switchTab(true);
        vnTabButtons.add(home);

        VnTabButton bill = binding.tabBill.loadTab(R.mipmap.ic_tab_bill_n, R.mipmap.ic_tab_bill_p,
                getString(R.string.tab_bill));
        bill.setOnClickListener(v -> switchTab(1));
        vnTabButtons.add(bill);

        VnTabButton mine = binding.tabMine.loadTab(R.mipmap.ic_tab_mine_n, R.mipmap.ic_tab_mine_p,
                getString(R.string.tab_mine));
        mine.setOnClickListener(v -> switchTab(2));
        vnTabButtons.add(mine);

        switchTab(0);

        // 业务埋点
        BaseApi.execBuriedPoint(2010, 2000, "主页", "主页");
    }

    private void switchTab(int tabIndex) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (preFragment == null) {
            preFragment = frags.get(0);
            preIndex = tabIndex;
            ft.add(R.id.container, preFragment, TabKey[tabIndex]).commit();
        } else {

            if (tabIndex == 1 || tabIndex == 2) {
                if (!VnContext.instance().isLogin()) {
                    VnUIManager.nextActivity(mActivity, SignInActivity.class);
                    return;
                }
            }

            for (int index = 0; index < vnTabButtons.size(); index++) {
                VnTabButton tv = vnTabButtons.get(index);
                tv.switchTab(index == tabIndex);
            }

            BaseFrag fg = frags.get(tabIndex);
            if (fg != preFragment) {
                if (fg.isAdded()) {
                    ft.hide(preFragment).show(fg).commitAllowingStateLoss();
                    fg.onResume();
                } else {
                    ft.hide(preFragment)
                            .add(R.id.container, fg, TabKey[tabIndex])
                            .commit();
                }
            }

            preIndex = tabIndex;
            preFragment = frags.get(tabIndex);
        }
    }

    public static void reOpen(Activity activity) {
        if (homeActivity != null) {
            homeActivity.finish();
            homeActivity = null;
        }

        Intent intent = new Intent();
        intent.setClass(activity, HomeActivity.class);
        activity.startActivity(intent);
    }

    public static void reloadHome() {
        if (homeActivity != null) {
            ((TabHomeFragment) homeActivity.frags.get(0)).reload();
        }
    }

    public static HomeActivity instance() {
        return homeActivity;
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
        new DialogWarring(mActivity)
                .setTitleText(R.string.label_warring)
                .setHintText(R.string.label_exit_app)
                .showConfirm(() -> {
                    super.onBackPressed();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocationHelper.getInstance().stopLocation();
    }
}